package com.example.qianfeng.image.stream;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;

public class JudgeImage {
    // 图片文件路径
    private final static File IMG_PATH = new File("D:\\pictures\\statics\\img");

    // 本目录及子目录下的文件
    private static List<String> allFile = new ArrayList<>();
    /**
     * 判断文件是否为图片
     *
     * @param name name
     * @return boolean
     */
    private static boolean isEndWithPic(String name) {
        String[] split = name.split("\\.");
        String suffix = split[1];
        switch (suffix) {
            case "jpg":
            case "jpeg":
            case "gif":
            case "png":
            case "JPG":
            case "JPEG":
            case "GIF":
            case "PNG":
                return true;
            default:
                return false;
        }
    }

    /**
     * 判断传入的文件是否是图片
     *
     * @param file file
     * @return boolean
     */
    public static boolean isImage(File file) {
        try {
            Image image = ImageIO.read(file);
            return image != null;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        // 获取所有目录及文件
        File[] files = IMG_PATH.listFiles();

        List<String> paths = new ArrayList<>();

        // 整理出所有的路径
        Map<String, Object> map = findAllPaths(files, paths);
        List<String> originPaths = (List<String>) map.get("paths");
        // 先遍历当前目录下的图片
        iterateCurrent(files);
        // 遍历子目录下的图片
        iterateSub(originPaths);
        // List<File> fileList = initFileList(files);

        // 提取出当前用户的图片信息
        List<String> subFileNameList = new ArrayList<>();
        for (String name : allFile) {
            if (name.indexOf("user001") > 0 && isImage(new File(name))) {
                subFileNameList.add(name);
            }
        }

        // 当前用户的图片遍历验证是否过期
        if (subFileNameList != null && subFileNameList.size() > 0) {
            handleOutTimeFile(subFileNameList);
        }


    }

    /**
     * 处理过期文件
     *
     * @param subFileNameList 文件名称列表
     */
    private static void handleOutTimeFile(List<String> subFileNameList) {
        String suffix = "user001";
        String time;
        Long timestamp = 0L;
        for (String name : subFileNameList) {
            if (name.contains(suffix)) {
                String[] strings = name.split(suffix);
                if (strings.length == 2) {
                    time = strings[1].substring(1, 15);
                    timestamp = ImageFormatChecker.getTimeStampSys(time);
                    executeOutTime(timestamp, name);
                } else if (strings.length > 2) {
                    time = strings[2].substring(1, 15);
                    timestamp = ImageFormatChecker.getTimeStampSys(time);
                    executeOutTime(timestamp, name);
                }
            }
        }
    }

    /**
     * 过期判断然后删除过期文件
     *
     * @param timestamp timestamp
     * @param name name
     */
    private static void executeOutTime(Long timestamp, String name) {
        Date date = new Date(Long.parseLong(String.valueOf(timestamp)));  // 获取图片上传日期
        // 当前日期
        Calendar calendar = Calendar.getInstance();
        Date nowTime = calendar.getTime();

        // 图片上传日期第30天的日期
        calendar.setTime(date);
        // 图片上传保存一个月检查
        calendar = ImageFormatChecker.initCalendar(calendar);
        Date outTime = calendar.getTime();
        if (nowTime.after(outTime)) { // 当前日期在过期日期之后
            executeDelete(name);
        }
    }

    /**
     * 清理图片
     *
     * @param name 图片名称
     */
    private static void executeDelete(String name) {
        System.out.println(name);
        if (name != null && name.length() > 0) {
            new File(name).delete();
        }
    }

    /**
     * 遍历子目录中的文件
     *
     * @param originPaths originPaths
     */
    private static void iterateSub(List<String> originPaths) {
        if (originPaths != null && originPaths.size() > 0) {
            for (int i = 0; i < originPaths.size(); i++) {
                File file = new File(originPaths.get(i));
                File[] listFiles = file.listFiles();
                for (int j = 0; j < listFiles.length; j++) {
                    if (!listFiles[j].isDirectory()) {
                        // System.out.println("子文件夹中---->执行判断文件是否过期进行删除!---->" +originPaths.get(i)+ "\\" +  listFiles[j].getName());
                        if (isImage(listFiles[j])) {
                            allFile.add(originPaths.get(i) + "\\" + listFiles[j].getName());
                        }
                    }
                }
            }
        }
    }

    /**
     * 遍历当前文件夹中的图片
     *
     * @param files files
     */
    private static void iterateCurrent(File[] files) {
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isDirectory()) {
                // System.out.println("当前文件夹中---->执行判断文件是否过期进行删除!"+files[i].getPath());
                if (isImage(files[i])) {
                    allFile.add(files[i].getPath());
                }
            }
        }
    }

    /**
     * 找到本目录及子目录中的所有路径
     *
     * @param files files
     * @param paths paths
     * @return Map
     */
    private static Map<String, Object> findAllPaths(File[] files, List<String> paths) {
        Map<String, Object> map = new HashMap<>(8);
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                paths.add(files[i].getPath());
            }
        }
        map.put("paths", paths);
        return map;
    }

    private static List<File> initFileList(File[] files) {
        List<File> fileList = new ArrayList<>();
        for (File file : files) {
            System.out.println("Check file:     " + file.getName() + " : " + isImage(file));
            // 如果是图片存入 list
            if (isImage(file)) {
                fileList.add(file);
            }
        }
        return fileList;
    }
}
